"""
Case Type   : 小型化
Case Name   : 关闭审计功能开关，审计文件丢失
Create At   : 2024/12/23
Owner       : wan005
Description :
    1.获取起止时间内的审计信息
    2.查看审计信息
    3.移除审计文件
    4.关闭审计开关
    5.再次获取起止时间内的审计信息
    6.查看审计信息
    7.开启审计开关
    8.查看审计文件是否生成
Expect      :
    1.审计信息获取成功
    2.查看到审计信息
    3.移除审计文件成功
    4.开关关闭
    5.获取到审计信息
    6.查看到审计信息
    7.审计开关打开成功
    8.审计文件生成
History     :
"""
import os
import time
import unittest
from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_node = Node('PrimaryRoot')
        self.user_node = Node('PrimaryDbUser')
        self.user = None
        self.port = None

    def test_audit(self):
        text = '----prestep1:获取可用用户和端口;expect:获取成功----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(
            self.root_node)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_node.ssh_user = self.user
        self.user_node.ssh_password = macro.COMMON_PASSWD
        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.audit_path = os.path.join(self.data_path, 'pg_audit')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, self.root_node)

        text = '----prestep3:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_node)
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----prestep4:执行脚本安装数据库;expect:安装成功----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_node, self.port)
        self.log.info(res)
        self.assertIn('start datanode success', res, f'执行失败: {text}')

        text = '----step1:获取起止时间内的审计信息;expect:获取成功----'
        self.log.info(text)
        text = '----获取起始时间----'
        self.log.info(text)
        date_cmd1 = f'SELECT sysdate;'
        start_cmd1 = f'gsql -d postgres -p {self.port} -c "{date_cmd1}"'
        self.log.info(start_cmd1)
        start_msg1 = self.user_node.sh(start_cmd1).result()
        start_time1 = start_msg1.splitlines()[-2].strip()
        self.log.info(start_time1)
        text = '----执行sql语句----'
        time.sleep(2)
        self.log.info(text)
        login_cmd1 = f'gsql -d postgres -p {self.port} -c "\\q"'
        self.log.info(login_cmd1)
        login_res1 = self.user_node.sh(login_cmd1).result()
        self.log.info(login_res1)
        time.sleep(6)
        text = '----获取结束的时间----'
        self.log.info(text)
        date_cmd2 = 'SELECT sysdate;'
        end_cmd1 = f'gsql -d postgres -p {self.port} -c "{date_cmd2}"'
        self.log.info(end_cmd1)
        end_msg1 = self.user_node.sh(end_cmd1).result()
        end_time1 = end_msg1.splitlines()[-2].strip()
        self.log.info(end_time1)

        text = '----step2.查看审计信息;expect:查看成功----'
        self.log.info(text)
        sql_cmd2 = f"select * from pg_query_audit('{start_time1}'," \
                   f"'{end_time1}') where username='{self.user}';"
        exe_cmd2 = f"gsql -d postgres -p {self.port} -c \"{sql_cmd2}\""
        self.log.info(exe_cmd2)
        res2 = self.user_node.sh(exe_cmd2).result()
        self.log.info(res2)
        self.assertIn('login_success', res2, '执行失败:' + text)
        self.assertIn('user_logout', res2, '执行失败:' + text)

        text = '----step3.移除审计文件;expect:成功----'
        self.log.info(text)
        rm_cmd = f'rm -rf {os.path.join(self.audit_path, "*_adt")}'
        self.log.info(rm_cmd)
        rm_res = self.user_node.sh(rm_cmd).result()
        self.log.info(rm_res)
        self.assertEqual('', rm_res, '执行失败:' + text)

        text = '----step4.关闭审计开关;expect:成功----'
        self.log.info(text)
        exe_cmd4 = f'gs_guc reload -D {self.data_path} -c \'audit_enabled=off\''
        exe_msg4 = self.user_node.sh(exe_cmd4).result()
        self.log.info(exe_msg4)
        self.assertIn('Success to perform gs_guc', exe_msg4, '执行失败:' + text)

        text = '----step5:再次获取起止时间内的审计信息;expect:获取成功----'
        self.log.info(text)
        text = '----获取起始时间----'
        self.log.info(text)
        sql_cmd5 = f'SELECT sysdate;'
        start_cmd2 = f'gsql -d postgres -p {self.port} -c "{sql_cmd5}"'
        self.log.info(start_cmd2)
        start_msg2 = self.user_node.sh(start_cmd2).result()
        start_time2 = start_msg2.splitlines()[-2].strip()
        self.log.info(start_time2)
        text = '----执行sql语句----'
        time.sleep(2)
        self.log.info(text)
        login_cmd5 = f'gsql -d postgres -p {self.port} -c "\\q"'
        self.log.info(login_cmd5)
        login_res5 = self.user_node.sh(login_cmd5).result()
        self.log.info(login_res5)
        time.sleep(6)
        text = '----获取结束的时间----'
        self.log.info(text)
        sql_cmd5_2 = 'SELECT sysdate;'
        end_cmd5_2 = f'gsql -d postgres -p {self.port} -c "{sql_cmd5_2}"'
        self.log.info(end_cmd5_2)
        end_msg2 = self.user_node.sh(end_cmd5_2).result()
        end_time2 = end_msg2.splitlines()[-2].strip()
        self.log.info(end_time2)

        text = '----step6.查看审计信息;expect:查看成功----'
        self.log.info(text)
        sql_cmd6 = f"select * from pg_query_audit('{start_time2}'," \
                   f"'{end_time2}') where username='{self.user}';"
        exe_cmd2 = f"gsql -d postgres -p {self.port} -c \"{sql_cmd6}\""
        self.log.info(exe_cmd2)
        res6 = self.user_node.sh(exe_cmd2).result()
        self.log.info(res6)
        self.assertNotIn('login_success', res6, '执行失败:' + text)
        self.assertNotIn('user_logout', res6, '执行失败:' + text)

        text = '----step7.打开审计开关;expect:成功----'
        self.log.info(text)
        exe_cmd7 = f'gs_guc reload -D {self.data_path} -c \'audit_enabled=on\''
        exe_msg7 = self.user_node.sh(exe_cmd7).result()
        self.log.info(exe_msg7)
        self.assertIn('Success to perform gs_guc', exe_msg7, '执行失败:' + text)

        text = '----step8.查看审计文件是否生成;expect:成功----'
        self.log.info(text)
        time.sleep(3)
        ls_file8 = f'ls {os.path.join(self.audit_path, "*adt")} |tail -1'
        ls_msg8 = self.user_node.sh(ls_file8).result()
        self.log.info(ls_msg8)
        self.assertEqual('_adt', ls_msg8[-4:], '执行失败:' + text)

    def tearDown(self):
        text = '-----清理环境-----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(self.user_node)
        self.com_uninstall.clear_env(self.root_node)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
